using System.Web.Services;
using System.Web;
using System.Xml;
using System;

using umlungu.engineX.blogX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-02-11
	Status:		beta	
	Version:	2.0.0
	Build:		20080507
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20080211:	Starting point of separation of Nav from site pages
	20080403:	Add the domain property
	20080414:	Added hint and hint checking broker calls
	20080421:	Changed the domain property to a object
	20080425:	Added splash (loads blog element into content)
	20080507:	Added webgames signon broker
	20080508:	Added FriendID parameter (for friend passing invite id's)
	20080512:	Changed setMenuActive to _SetMenuActive for WebmaiNavbarX
	20080512:	Added broker mechanism for SiteContact
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX.WebmailX {
	/// <summary>
	/// Description of the classX class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class WebmailProfileX : profileX {
		#region Invisible properties
		private x_broker broker;
		#endregion

		#region Constants
		private const string INTERSTITIAL_URL = "interstitial.aspx";
		#endregion

		#region Visible properties
		private WebmailXCookies cooker;
		/// <summary>The WebmailX cookie manager object</summary>
		public WebmailXCookies Cooker {
			get { return cooker; }
		}
		private WebmailXDomain domain;
		/// <summary>The webmail domain object</summary>
		public WebmailXDomain Domain {
			get { return domain; }
		}
		private WebmailXUser user;
		/// <summary>The webmail user object</summary>
		public WebmailXUser WebmailUser {
			get { return user; }
		}
		#endregion

		#region Constructors/Destructors
		/// <summary>Default constructor</summary>
		public WebmailProfileX() : base("Webmail") {
			cooker = new WebmailXCookies(this);
		}
		/// <summary>Constructor for derived classes</summary>
		public WebmailProfileX(string label) : base(label) {
			cooker = new WebmailXCookies(this);
		}
		#endregion

		#region Public methods
		#endregion
		
		#region Protected methods
		protected override void _Initialise() {
			base._Initialise();
			user = new WebmailXUser(UserProfile);
			domain = new WebmailXDomain(this);
		}

		/// <summary>Initialises the page and </summary>
		protected override void _InitialiseTemplate() {
			base._InitialiseTemplate();
			Debugger.Spoor("_InitialiseTemplate", "skin", Cooker.Skin, "email", user.Username, "domain", domain.Name, "fullname", user.Fullname);
			TemplateArguments.AddParam("skin", "", Cooker.Skin);
			TemplateArguments.AddParam("user", "", user.Username);
			TemplateArguments.AddParam("person", "", user.Fullname);
			TemplateArguments.AddParam("domain", "", domain.Name);
			/* not needed from 20080513...
			TemplateArguments.AddParam("friendid", "", user.FriendID);
			TemplateArguments.AddParam("fileid", "", user.FileID);
			*/
			TemplateArguments.AddParam("interstitial", "", String.Concat("http://", Request.Url.Host, "/", INTERSTITIAL_URL));
		}
		/// <summary>
		/// Does the pre-processing of the page request. 
		///	This method is overriden in order to:
		///	- Add form fields to the profile
		/// </summary>
		protected override void _ProcessRequest() {
			Debugger.Spoor("_ProcessRequest", "Broker", Parameters.Broker);
			Cooker.Read((Parameters.Broker != "passportlogin"));
			setWizard();
			UserProfile.Add(Request);
			_CheckPattern(true);	// check for pattern
			switch (Parameters.Broker) {
				case "feedbackbeta":		broker = new WebmailXFeedback(this, "beta");	broker.Process();	break;
				// WebmailPassportX related brokers
				case "passporttest":		broker = new WebmailPassport(this);	broker.Process("test");			break;
				case "passportlogin":		broker = new WebmailPassport(this);	broker.Process("login");		break;
				case "passportregister":	broker = new WebmailPassport(this);	broker.Process("register");		break;
				case "passportchange":		broker = new WebmailPassport(this);	broker.Process("change");		break;
				case "passportforgot":		broker = new WebmailPassport(this);	broker.Process("forgot");		break;
				case "passportsummary":		broker = new WebmailPassport(this);	broker.Process("summary");		break;
				case "passportunlock":		broker = new WebmailPassport(this);	broker.Process("unlock");		break;
				case "passportlogout":		broker = new WebmailPassport(this);	broker.Process("logout");		break;
				case "passporttoken":		broker = new WebmailPassport(this); broker.Process("token");		break;
				case "passporthint":		broker = new WebmailPassport(this); broker.Process("hint");			break;
                case "passportcheckhint":   broker = new WebmailPassport(this); broker.Process("checkhint");    break;
				case "passportshare":		broker = new WebmailPassport(this); broker.Process("share");		break;
				// WebmailShare related brokers
				case "shareinvite":			broker = new WebmailShare(this);	broker.Process("invite");		break;
				case "shareaccept":			broker = new WebmailShare(this);	broker.Process("accept");		break;
				case "sharethank":			broker = new WebmailShare(this);	broker.Process("thank");		break;
                // Site broker
				case "siteskin":			broker = new WebmailSite(this);		broker.Process("skin");			break;
				// Webgames broker
				case "webgamessignon":		broker = new WebmailGames(this);	broker.Process();				break;
				// Tests
				case "errortest":		throw new x_exception("error_test", "This is an error test");
				case null:
				case "":				break;
				// use base broker for default (ie sends email on submission)
				case "SiteContact":
				default:				broker = new x_broker(this);			broker.Process();	break;
			}
		}
		/// <summary>
		/// Does the post-processing of the page content. 
		///	This method is overriden in order to:
		///	- set the default on the menu tab
		///	- post process the xml content through a broker (ie extend xml added via page.xml)
		/// </summary>
		protected override void _ProcessContent() {
			Debugger.Spoor("_ProcessContent");
			XmlElement menuitem = _SetMenuActive();
			if (menuitem != null) {
				WebmailXAdverts webads = new WebmailXAdverts(Config);
				try {
					Debugger.Spoor("_ProcessContent", "menuitem", menuitem.GetAttribute("link"), "adtyp", menuitem.GetAttribute("adtyp"));
					webads.Get(menuitem);
					Debugger.Spoor("_ProcessContent", "webads", webads.DocumentElement.OuterXml);
					Content.AppendChild(Document.ImportNode(webads.DocumentElement, true));
				}
				catch { }

				XmlNode splashNode = Parameters.Link.SelectSingleNode("splash");
				if (splashNode != null) {
					try {
						Debugger.Spoor("_ProcessContent", "splash", splashNode.OuterXml);
						WebmailSplash splash = new WebmailSplash(this, splashNode as XmlElement);
						splash.Get();
						XmlNode SplashContent = Content.AppendChild(Document.CreateElement("Splash"));
						SplashContent.AppendChild(Document.ImportNode(splash.Content, true));
					}
					catch { }
				}
			} 
			switch (Parameters.Broker) {
				// Webgames broker
				case "webgamessignon":	((WebmailGames)broker).ProcessContent(Content);	break;
			}
			//else
			//	Debugger.Spoor("_ProcessContent", "menuitem", "null");
			Cooker.Write(!user.Remember);
		}
		protected XmlElement _SetMenuActive() {
			string active = "", menuid = "";
			string selitm = String.Concat("menuitem[@link='", Parameters.LinkName, "']");
			XmlNode activeitem = null;
			XmlElement menus = Content.SelectSingleNode("//menus") as XmlElement;
			foreach (XmlNode menu in menus.SelectNodes("menu[not(@display) or @display!='no']")) {
				menuid = ((XmlElement)menu).GetAttribute("id");
				XmlNode menuitem = menu.SelectSingleNode(selitm);
				if (menuitem != null) {
					active = menuid;
					activeitem = menuitem;
				}
			}
			Debugger.Spoor("_SetMenuActive", "LoginPattern", UserProfile.LoginPattern, "active", active);
			if (active != "") {
				menus.SetAttribute(String.Concat("default_", UserProfile.LoginPattern), active);
			}
			return activeitem as XmlElement;
		}

		#endregion
				
		#region Private methods
		private void setWizard() {
			string wizard = this.GetQueryParam("wizard");
			if (wizard != "") {
				Cooker.Wizard = wizard;
			}
		}
		#endregion
	}
}
